﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AppMain
{
    public class Monster : ActionRole
    {

        private BehaviorTree.MonsterTree AI = null;
        private float aiTickTime = 0f;
        private float aiTickInterval = 0.5f;
        public Monster(IAttrMap attrMap) : base(attrMap)
        {
            AI = new BehaviorTree.MonsterTree(this);
            this.AttrMap.changeAttr<float>(RoleAttr.RestTime, 11f);
        }

        public void inputMove(float x, float z)
        {
            if (x != 0)
            {
                int status = x > 0 ? 1 : -1;
                this.AttrMap.changeAttr<int>(RoleAttr.LookFlag, status);
            }

            float speed = this.AttrMap.getAttr<float>(RoleAttr.Speed).Value;
            float zSpeed = speed * 0.35f;
            float xMove = x * speed;
            float zMove = z * zSpeed;
            if (x == 0 && z == 0)
            {
                this.moveWidget.doMove(new MoveData() { moveStatus = Move_Status.SelfStand });
                //广播给场景中玩家
                BaseScene scene = this.AttrMap.getAttr<BaseScene>(RoleAttr.Scene).Value;
                if (scene != null)
                {
                    pb.RoleMoveBroadCastMsg msg = new pb.RoleMoveBroadCastMsg();
                    msg.roleId = this.UID;
                    msg.status = 0;
                    msg.posx = this.box3D.posX;
                    msg.posy = this.box3D.posY;
                    msg.posz = this.box3D.posZ;
                    msg.dirx = 0;
                    msg.dirz = 0;
                    scene.broadCast(Cmd.RoleMoveBroadCastMsg, msg);
                }
            }
            else
            {
                MoveData dt = MoveData.New();
                dt.dir.x = xMove;
                dt.dir.z = zMove;
                dt.moveStatus = Move_Status.SelfRun;
                this.moveWidget.doMove(dt);
            }
        }

        public override void tick(float frameTime)
        {
            base.tick(frameTime);
            aiTickTime += frameTime;
            if (aiTickTime >= aiTickInterval)
            {
                AI.tick(aiTickTime);
                aiTickTime = 0;
            }
        }

    }
}
